package com.micro.school.common.core.utils;

import com.micro.school.common.core.context.SecurityContextHolder;

import java.util.Arrays;

public class SecurityContextHolderUtils {

    public static int[] getCampusId(){
        // 添加空值检查
        String userCampusId = SecurityContextHolder.getUserCompusId();
        if (userCampusId == null || userCampusId.isEmpty()) {
            return null;
        }

        if (userCampusId.contains(",")){
            String campusIdsStr = userCampusId;
            int[] campusIds = Arrays.stream(campusIdsStr.split(","))
                    .map(String::trim)
                    .filter(StringUtils::isNotEmpty)
                    .mapToInt(Integer::parseInt)
                    .toArray();
            return campusIds;
        }else{
            return new int[]{Integer.parseInt(userCampusId)};
        }
    }

    public static int[] getCollegeId(){
        // 添加空值检查
        String userCollegeId = SecurityContextHolder.getUserCollegeId();
        if (userCollegeId == null || userCollegeId.isEmpty()) {
            return null;
        }
        if (userCollegeId.contains(",")){
            String collegeId = userCollegeId;
            int[] campusIds = Arrays.stream(collegeId.split(","))
                    .map(String::trim)
                    .filter(StringUtils::isNotEmpty)
                    .mapToInt(Integer::parseInt)
                    .toArray();
            return campusIds;
        }else{
            return new int[]{Integer.parseInt(userCollegeId)};
        }
    }
}
